<?php
require_once(dirname(__FILE__).'/../config.php');
require_once('weixin.class.php');
$weixin = new class_weixin();
if(!isset($_GET["code"]))
{
	$redirect_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
	$jumpurl = $weixin->oauth2_authorize($redirect_url, "snsapi_userinfo", "123");
	Header("Location: $jumpurl");
}
else
{
	$access_token_oauth2 = $weixin->oauth2_access_token($_GET["code"]);
	$userinfo = $weixin->get_user_info($access_token_oauth2['openid']); //需关注获取方法
	if($userinfo["subscribe"]==0)
	{
		$userinfo = $weixin->oauth2_get_user_info($access_token_oauth2['access_token'], $access_token_oauth2['openid']);
	}
}
$openid		=	$userinfo["openid"];
$logincook	=	315360000;
$userpic	=	$face = str_replace('http:', '', $userinfo[headimgurl]);
$usernamez	=	''.$userinfo["nickname"].'';
$username	=	$nickname = $weixin->filter($usernamez);
$address	=	"".$userinfo[country]."".$userinfo[province]."".$userinfo[city]."";
$gzzt		=	$userinfo["subscribe"];
$wxfollow	=	$userinfo["subscribe"];
if ($userinfo["sex"] == 1) {
	$sex = '男';
} else {
	$sex = '女';
}
$mtype = '个人';
$dfscores = 0;
$dfmoney = 0;
$dfrank = $dsql->GetOne("SELECT money,scores FROM `#@__arcrank` WHERE rank='10' ");
if (is_array($dfrank)) {
$dfmoney = $dfrank['money'];
$dfscores = $dfrank['scores'];
}
$jointime = $logintime = time();
$joinip =  $loginip = GetIP();
$spaceSta = 0;
$_SESSION['jj_openid'] = $openid;
$_SESSION['jj_nickname'] = $nickname;
$_SESSION['jj_face'] = $face;
$_SESSION['jj_sex'] = $sex;
if(!$wxfollow)
{
	$wxfollow=0;
}
if($openid)
{
	$uarr = $dsql->GetOne("select * from #@__member where `jjwxopenid` = '$openid'");
	if (is_array($uarr)) { //存在即登陆
		$cfg_ml->PutLoginInfo($uarr['mid']);
		$cfg_ml->DelCache($uarr['mid']);
		Header("Location: /");
		exit;
	} else { //不存在 看用户是否已登录
		//如果已经登录了
		if (!empty($cfg_ml->M_ID)) {
		  //绑定微信 
		  $sql = "update #@__member set `jjwxopenid` = '" . $_SESSION['jj_openid'] . "',`uname` = '" . $_SESSION['jj_nickname'] . "',`face` = '" . $_SESSION['jj_face'] . "',`sex` = '" . $_SESSION['jj_sex'] . "' where mid = '" . $cfg_ml->M_ID . "' ";
		  $dsql->ExecuteNoneQuery($sql);
		  $_SESSION['jj_openid'] = '';
		  $_SESSION['jj_nickname'] = '';
		  $_SESSION['jj_face'] = '';
		  $_SESSION['jj_sex'] = '';
		  ShowMsg("微信绑定成功...", "/", 0, 1000);
		  exit;
		} else { //注册新用户
		  $userid = 'wx' . rand(10000000, 99999999);
		  $pwd = md5('123456');
		  //推荐人
		  $tjrmid = GetCookie('tjrmid');
		  if (empty($tjrmid)) {
			$tjrmid = 0;
		  }
		  $inQuery = "INSERT INTO `#@__member` (`mtype` ,`userid` ,`uname`,`pwd`  ,`sex` ,`rank` ,`money`  ,`scores` , `matt`,
			`spacesta` ,`face`,`safequestion`,`safeanswer` ,`jointime` ,`joinip` ,`logintime` ,`loginip`,`jjwxopenid`,`tjrmid` )    
			   VALUES ('$mtype','$userid','$nickname','$pwd','$sex','10','$dfmoney','$dfscores',     
			 '0','$spaceSta','$face','$safequestion','$safeanswer','$jointime','$joinip','$logintime','$loginip','$openid','$tjrmid'); ";
		  $dsql->ExecuteNoneQuery($inQuery);
		  $mid = $dsql->GetLastID();
		  $space = 'person';
		  $membertjquery = "INSERT INTO `#@__member_tj` (`mid`,`article`,`album`,`archives`,`homecount`,`pagecount`,`feedback`,`friend`,`stow`)
					   VALUES ('$mid','0','0','0','0','0','0','0','0'); ";
		  $dsql->ExecuteNoneQuery($membertjquery);
		  $spacequery = "INSERT INTO `#@__member_space`(`mid` ,`pagesize` ,`matt` ,`spacename` ,`spacelogo` ,`spacestyle`, `sign` ,`spacenews`)
					VALUES('{$mid}','10','0','{$uname}的空间','','$space','',''); ";
		  $dsql->ExecuteNoneQuery($spacequery);
		  $dsql->ExecuteNoneQuery("INSERT INTO `#@__member_flink`(mid,title,url) VALUES('$mid','果泽森','https://www.guozesen.cn'); ");

		  /*
		  require_once DEDEINC . '/membermodel.cls.php';
		  $membermodel = new membermodel($mtype);
		  $modid = $membermodel->modid;
		  $modid = empty($modid) ? 0 : intval(preg_replace("/[^\d]/", '', $modid));
		  $modelform = $dsql->getOne("SELECT * FROM #@__member_model WHERE id='$modid' ");
		  $dsql->ExecuteNoneQuery("INSERT INTO `{$membermodel->table}` (`mid`) VALUES ('{$mid}');");
         */

		  $cfg_ml->PutLoginInfo($mid);
		  $cfg_ml->DelCache($mid);
		  Header("Location: /");
		}
	}
}
?>